package pl.umk.mat.fred.unicore.urc.storagemanagement.actions;

import java.io.IOException;
import java.util.Random;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import de.fzj.unicore.uas.client.RByteIOClient;
import de.fzj.unicore.uas.client.StorageClient;

import pl.umk.mat.fred.unicore.urc.storagemanagement.tools.StorageTools;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.CatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.GridNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.MultiplyCatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.Node;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.StorageNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.unicore.Storage;
import pl.umk.mat.fred.unicore.urc.storagemanagement.views.StorageBrowserView;

import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;

public class CreateNewFileAction extends Action {

	private StorageBrowserView view;
	
	public CreateNewFileAction(StorageBrowserView view) {
		this.view = view;
		this.setText("Create file");
	}
	
	public void run() {
		InputDialog dialog = new InputDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
				"File name", "Enter file name", null, null);
		dialog.open();
		if (dialog.getReturnCode() == 0) {
			Storage client = null;
			String path = null;
			final IStructuredSelection selection = (IStructuredSelection) view.getTreeViewer().getSelection();
			if (selection.getFirstElement() instanceof StorageNode) {
				StorageNode node = (StorageNode) selection.getFirstElement();
				client = node.getStorage();
				path = node.getPath();
			} else if (selection.getFirstElement() instanceof CatalogNode) {
				CatalogNode node = (CatalogNode) selection.getFirstElement();
				client = node.getStorage();
				path = node.getPath();
			} else if (selection.getFirstElement() instanceof GridNode) {
				client = StorageTools.randomStorage((GridNode)selection.getFirstElement());
				path = "/";
			} else if (selection.getFirstElement() instanceof MultiplyCatalogNode) {
				CatalogNode cn = StorageTools.randomCatalogNode((MultiplyCatalogNode) selection.getFirstElement());
				client = cn.getStorage();
				path = cn.getPath();
			}
				
			final String path2 = path;
			final Storage client2 = client;
			final String dialogValue = dialog.getValue();
			if ((path != null) && (client != null)) {
				Job job = new Job("Creating new file") {
					@Override
					protected IStatus run(IProgressMonitor monitor) {
						monitor.beginTask("Creting a file", IProgressMonitor.UNKNOWN);
						try {
							String p = URIUtil.encode(path2 + "/" + dialogValue, org.apache.commons.httpclient.URI.allowed_fragment);
							RByteIOClient ioClient = client2.getImport(p);
							ioClient.write(new byte[0]);
							ioClient.destroy();
						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							view.getDoubleClickAction().run((Node) selection.getFirstElement());
							monitor.done();
						}
						return org.eclipse.core.runtime.Status.OK_STATUS;
					}
				};
				job.schedule();
			}
		}
	}
}
